import ChkNum
from util import check


def writeSql(s):
    f = open("/Users/demon/Desktop/1/phone.sql", "a")
    f.write('%s\n' % s.strip())
    f.close()


def write(s):
    f = open("/Users/demon/Desktop/1/local.txt", "a")
    f.write('%s\n' % s.strip())
    f.close()


def deal_sql():
    file = open("/Users/demon/Desktop/1/phoneNum.txt", "r")
    index = 0
    sql = "insert into wanda_user(`phone`,`token`,`member_id`,`createtime`) values "
    while True:
        index += 1
        mystr = file.readline()
        if not mystr:
            break
        result = mystr.split(" ")
        print(result)
        phone = result[2]
        code = result[6].split("|")[1]
        memberId = result[7].split(":")[1]
        token = result[8].split(":")[1]
        createtime = result[12].split(":")[1] + " " + result[13].strip()
        # if (code == "Failed"):
        #     continue
        # r = check.check(phone)
        # local = r["data"]["items"][0]["fromOrg"]
        # result = mystr.split("|")
        # phone = result[0]
        # code = result[1]
        # r = check.check(phone)
        # if r["data"]["totalCount"] == 0:
        #     continue
        # local = r["data"]["items"][0]["fromOrg"]
        # p = "%s|%s|%s" % (phone, code, local)
        # write(p)
        sql += "('%s','%s','%s','%s')," % (phone, token, memberId, createtime)
    writeSql(sql)


def deal_local():
    file = open("fan/满洲里20181130.txt", "r")
    index = 0
    while True:
        index += 1
        mystr = file.readline()
        if not mystr:
            break
        result = mystr.split("|")
        print(result)
        phone = result[0]
        r = check.check(phone)
        if r["data"]["totalCount"] == 0:
            continue
        local = r["data"]["items"][0]["fromOrg"]
        p = "%s|%s" % (phone, local)
        write(p)


if __name__ == '__main__':
    deal_local()
